package com.xuecheng.manage_cms_client.serivce;

import com.mongodb.client.gridfs.GridFSBucket;
import com.mongodb.client.gridfs.GridFSDownloadStream;
import com.mongodb.client.gridfs.model.GridFSFile;
import com.xuecheng.framework.domain.cms.CmsPage;
import com.xuecheng.framework.domain.cms.CmsSite;
import com.xuecheng.framework.domain.cms.response.CmsCode;
import com.xuecheng.framework.exception.ExceptionCast;
import com.xuecheng.manage_cms_client.dao.CmsMongoDbDao;
import com.xuecheng.manage_cms_client.dao.SiteMongoDbDao;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.gridfs.GridFsResource;
import org.springframework.data.mongodb.gridfs.GridFsTemplate;
import org.springframework.stereotype.Service;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

/**
 * @author ShengJinFeng
 * @version V1.0
 * @ClassName PageService
 * @Description (这里用一句话描述这个类的作用)
 * @date 2020/1/30 14:17
 * @since V1.0
 */
@Service
public class PageService {
    
    @Autowired
    private CmsMongoDbDao cmsMongoDbDao;
    
    @Autowired
    private SiteMongoDbDao siteMongoDbDao;

    @Autowired
    private GridFsTemplate gridFsTemplate;

    @Autowired
    private GridFSBucket gridFSBucket;
    
    /**
     * @Description 将页面html保存到页面物理路径
     * @author ShengJinFeng
     * @date 2020/1/30
     */
    public void savePageToServerPath(String pageId){
        CmsPage cmsPage = cmsMongoDbDao.queryById(pageId);
        if (cmsPage == null){
            ExceptionCast.cast(CmsCode.CMS_PAGE_NOTEXISTS);
        }
        CmsSite cmsSite = siteMongoDbDao.queryById(cmsPage.getSiteId());
        if (cmsSite == null){
            ExceptionCast.cast(CmsCode.CMS_SITE_NOTEXISTS);
        }
        String pagePath = cmsSite.getSitePhysicalPath()+cmsPage.getPagePhysicalPath()+cmsPage.getPageName();
        InputStream inputStream = getFileById(cmsPage.getHtmlFileId());
        if (inputStream == null){
            ExceptionCast.cast(CmsCode.CMS_GENERATEHTML_HTMLISNULL);
        }
        FileOutputStream fileOutputStream = null;
        try{
            fileOutputStream = new FileOutputStream(new File(pagePath));
            IOUtils.copy(inputStream,fileOutputStream);
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            try {
                inputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                fileOutputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * @Description 根据文件id获取文件内容
     * @author ShengJinFeng
     * @date 2020/1/30
     */
    private InputStream getFileById(String fileId){
        try {
            GridFSFile gridFSFile = gridFsTemplate.findOne(Query.query(Criteria.where("_id").is(fileId)));
            GridFSDownloadStream gridFSDownloadStream = gridFSBucket.openDownloadStream(gridFSFile.getObjectId());
            GridFsResource gridFsResource = new GridFsResource(gridFSFile,gridFSDownloadStream);
            return gridFsResource.getInputStream();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
}
